<?php
	class ArticleTable
	{
		private $items;
		private $cols;
		private $title;
		private $currentPage;
		private $nOfPages;
		private $canGoBack;
		private $canGoNext;
		
		public function __construct($title, $items, $currentPage, $nOfPages)
		{
			$this->title = $title;
			$this->items = $items;
			$this->currentPage = $currentPage;
			$this->nOfPages = $nOfPages;
			
			$this->cols = 3;
			
			$this->canGoBack = trim($currentPage) > 1;
			$this->canGoNext = trim($currentPage) < trim($nOfPages);
		}
		
		public function GetTable()
		{	
			$tdback = "<td></td>";
			$tdnext = "<td></td>";
			if ($this->canGoBack)
				$tdback = "<td id='back' class='tbnav' onmouseover=\"this.style.backgroundColor='#E5E5E5'\" onmouseout=\"this.style.backgroundColor='white'\"><img src='img/sback.png'></td>";
			if ($this->canGoNext)
				$tdnext = "<td id='next' class='tbnav' onmouseover=\"this.style.backgroundColor='#E5E5E5'\" onmouseout=\"this.style.backgroundColor='white'\"><img src='img/snext.png'></td>";
			
			echo("
				<div id='ajax'>
					<b>$this->title</b>
					<div class='loading'></div>
					<table width='100%'>
						<tr>
							$tdback
							<td>
								<table id='ajaxcontent'>
				");
				
			$this->HorizontalLineCell(false, false);			
			echo("<tr>");
    						
			$p = 0;	
			$i = 0;
			while ($i < count($this->items))
			{
				$this->ItemCell($this->items[$i]);
				$p++;
				$i++;
				if ($this->cols > $p)
				{
					$this->VerticalLineCell();
					if ($i == count($this->items))
					{
						$r = $this->cols - $p;
						
						for ($j = 0; $j < $r; $j++)
						{
							$this->ItemCell(null);
							if (($j + 1) < $r)
								$this->VerticalLineCell();
						}
						
						$this->HorizontalLineCell(true, true);
					}
				}	
				else
				{
					$this->HorizontalLineCell(true, true);
					$p = 0;
				}	
			}
			echo("
								</table>
							</td>
							$tdnext
						</tr>
					</table>	
					<span>page <span id='pagenum'>$this->currentPage</span> of $this->nOfPages</span>	 
				</div>					
				");		
		}
		
		public function ItemCell($item)
		{
			if (is_null($item))
			{
				echo("
					<td valign='top' width='33%' align='center'></td> 
				");
			}
			else
			{
				$name = $item->GetName();
				$image = $item->GetImage();
				echo("
					<td valign='top' width='33%' align='center'> 
				        <table>
				          <tr>
				            <td valign='top' align='center'>
				              <a href='' title=''>
				                <img src='img/$image' border='0' alt=/>
				              </a>
				            </td>
				          </tr>
				          <tr>
				            <td>
				              <a href='' class='productnamecolor colors_productname'>
				                $name
				              </a>
				              <br/>
				              <font class='text colors_text'>
				                <b>Our Price:</b> $60.00
				              </font>
				            </td>
				          </tr>
				        </table>
			      	</td> 
				");
			}
		}
		
		public function VerticalLineCell()
		{
			echo("
				<td rowspan='2' background='img/V.gif'> 
    				<img src='img/clear.gif' width='25' height='25'/> 
  			 	</td>
 			");
		}
		
		public function HorizontalLineCell($space, $closeRow)
		{
			if ($closeRow) echo ("</tr>");
			
			if ($space == true){
				echo("
					<tr> 
						<td valign='top' width='33%'> </td> 
						<td valign='top' width='33%'> </td> 
						<td valign='top' width='33%'> </td> 
					</tr>"
				);
			}
				
			echo("
				<tr> 
					<td background='img/H.gif' colspan='5'> 
						<img src='img/clear.gif' width='3' height='9'> 
					</td> 
				</tr> 
			");
		}
	}
?>
